//
//  LeftMessageTableViewCell.m
//  QQChatDemo
//
//  Created by zkingsoft on 16/1/11.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//

#import "LeftMessageTableViewCell.h"

#import "NSString+SizeForFont.h"
#import "UIImage+ResizeImage.h"

@implementation LeftMessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.backgroundView = nil;
        
        // 初始化time视图
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.textColor = [UIColor grayColor];
        self.timeLabel.font = [UIFont systemFontOfSize:14 weight:2.0];
        [self.contentView addSubview:_timeLabel];
        
        // 创建头像视图
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.layer.cornerRadius = 20;
        self.iconImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.iconImageView];
        
        // 创建内容message视图
        self.textView = [UIButton buttonWithType:UIButtonTypeCustom];
        self.textView.titleLabel.numberOfLines = 0;// 多行显示
        self.textView.titleLabel.font = [UIFont systemFontOfSize:17];
        [self.textView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.textView.contentEdgeInsets = UIEdgeInsetsMake(TEXT_PADDING, TEXT_PADDING, TEXT_PADDING, TEXT_PADDING);
        [self.contentView addSubview:self.textView];
        
        
        // 时间视图frame
        self.timeLabel.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .heightIs(40);
        
        // 头像视图frame 要根据type的类型 设置frame
        self.iconImageView.sd_layout
        .topSpaceToView(self.timeLabel, 1)
        .leftSpaceToView(self.contentView, 10)
        .widthIs(40)
        .heightIs(40);
    }
    return self;
}

- (void)setModel:(MessageModel *)model{
    _model = model;
    
    _timeLabel.text = model.timeString;
    [_textView setTitle:model.textString forState:UIControlStateNormal];
    _modelType = model.modelType;
    _iconImageView.image = [UIImage imageNamed:@"other.png"];
    
    
    // 内容message的视图  这个只能写在这了 因为写在其他地方 不知道数据 不能自适应 只有model传进来的时候model才有值model的textString才有数据 才可以进行自适应
    CGSize textMaxSize = CGSizeMake(150, MAXFLOAT);// 宽最宽为150 高无限制
    CGSize textSize = [model.textString sizeWithFont:[UIFont systemFontOfSize:17.0] maxSize:textMaxSize];
    CGSize textRealSize = CGSizeMake(textSize.width + TEXT_PADDING*2, textSize.height + TEXT_PADDING*2);
    
    self.textView.sd_layout
    .topSpaceToView(self.timeLabel, 10)
    .leftSpaceToView(self.contentView, 40 + TEXT_PADDING)
    .widthIs(textRealSize.width)
    .heightIs(textRealSize.height);
    [self.textView setBackgroundImage:[UIImage resizeImage:@"chat_recive_press_pic@2x.png"] forState:UIControlStateNormal];
    
    [self setupAutoHeightWithBottomView:self.textView bottomMargin:TEXT_PADDING];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
